using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerMove : MonoBehaviour
{
    public Camera followCamera;
    public float speed = 15.0f;
    public float zoomSpeed = 400f;

    private Rigidbody2D rig;
    private float axisX, axisY, cameraAxisZ;
    private Animator animator;
    private Vector2 lookDirection= new Vector2(0, -1);

    private void Awake()
    {
        //Time.timeScale = 3f;
    }
    void Start()
    {
        rig = GetComponent<Rigidbody2D>();
        animator=GetComponent<Animator>(); 
    }

    // Update is called once per frame
    void Update()
    {
        axisX = Input.GetAxis("Horizontal");
        axisY = Input.GetAxis("Vertical");
        cameraAxisZ = Input.GetAxis("Mouse ScrollWheel");

        Vector2 move = new Vector2(axisX, axisY);
        animator.SetFloat("X", move.x);
        animator.SetFloat("Y", move.y);
        if (!Mathf.Approximately(move.x, 0.0f) || !Mathf.Approximately(move.y, 0.0f)) {
            lookDirection.Set(move.x, move.y);
            lookDirection.Normalize();
        }
        animator.SetFloat("LookX", lookDirection.x);
        animator.SetFloat("LookY", lookDirection.y);
        animator.SetFloat("Speed", move.magnitude);


    }
    private void FixedUpdate()
    {
        float deltaTime = Time.deltaTime;
        Vector2 position = rig.transform.position;
        position += new Vector2(axisX * speed * deltaTime, axisY * speed * deltaTime);
        rig.MovePosition(position);
        //rig.transform.position = position;

        if (followCamera != null)
        {
            Vector3 cameraPosition = followCamera.transform.position;
            float z = Mathf.Clamp(cameraPosition.z+cameraAxisZ * zoomSpeed* deltaTime, -20,-3);
            cameraPosition.Set(position.x, position.y, z);
            followCamera.transform.position = cameraPosition;
        }
    }
}
